﻿//菜单绑定
function MenuBind() {
    $.ajax({
        url: "../../../ButtonConfigInfo/ButtonInfo.ashx?t=" + new Date().getDate().toString(),
        dataType: "text",
        type: "post",
        async: false,
        data: { stype: "getMenu", ShowUrl: "true", ShowIcon: "true" },
        success: function (data) {
            var dataJson = window.eval("(" + data + ")");
            if (dataJson.state) {
                $("#ul_Tree").html("");
                var setting = {};
                var zNodes = dataJson.rows;
                for (var i = 0; i < zNodes.length; i++) {
                    var menuChild = $("<li>");
                    menuChild.addClass("nav-header hidden-tablet").attr("OID", zNodes[i].OID);
                    menuChild.html(zNodes[i].name);
                    $("#ul_Tree").append(menuChild);
                    MenuLoad(zNodes[i].children);
                }
            }
        },
        error: function (i) {
            alert("出现异常，请刷新再试！");
        }
    });
}
//菜单树加载
function MenuLoad(zNodes) {
    if (zNodes) {
        for (var i = 0; i < zNodes.length; i++) {
            var menuChild = $("<li>");
            var childA = $("<a>").addClass("ajax-link").attr("href", zNodes[i].url).attr("target", zNodes[i].target);
            var childI = $("<i>").addClass("icon-home");
            //.css("background-image", "url(" + zNodes[i].icon + ")");
            var childText = $("<span>").addClass("hidden-tablet").html(" " +zNodes[i].name);
            menuChild.append(childA.append(childI).append(childText));
            $("#ul_Tree").append(menuChild);
            MenuLoad(zNodes[i].children);
        }
    }   
}
$(function () {
    //菜单树加载
    MenuBind();
    var current_theme = $.cookie('current_theme') == null ? 'cerulean' : $.cookie('current_theme');
    switch_theme(current_theme);
    $('#themes a[data-value="' + current_theme + '"]').find('i').addClass('icon-ok');
    $('#themes a').click(function (e) {
        e.preventDefault();
        current_theme = $(this).attr('data-value');
        $.cookie('current_theme', current_theme, { expires: 365 });
        switch_theme(current_theme);
        $('#themes i').removeClass('icon-ok');
        $(this).find('i').addClass('icon-ok');
        $("#DemoContext").attr("src", $("#DemoContext").attr("src"));
    });

    $(window).resize(function () {
        var screenHW = [(document.documentElement.clientHeight || document.body.clientHeight), (document.documentElement.clientWidth || document.body.clientWidth)];
        $('#DemoContext').height(screenHW[0] - $("header").height() - 20);
    });
    $(window).resize();
    function switch_theme(theme_name) {
        $('#bs-css').attr('href', 'css/bootstrap-' + theme_name + '.css');
    }

    //ajax menu checkbox
    $('#is-ajax').click(function (e) {
        $.cookie('is-ajax', $(this).prop('checked'), { expires: 365 });
    });
    $('#is-ajax').prop('checked', $.cookie('is-ajax') === 'true' ? true : false);

    //disbaling some functions for Internet Explorer
    if ($.browser.msie) {
        $('#is-ajax').prop('checked', false);
        $('#for-is-ajax').hide();
        $('#toggle-fullscreen').hide();
        $('.login-box').find('.input-large').removeClass('span10');
    }

    $('ul.main-menu li a').each(function () {
        if ($($(this))[0].href == String(window.location))
            $(this).parent().addClass('active');
    });
    var 
		History = window.History, // Note: We are using a capital H instead of a lower h
		State = History.getState(),
		$log = $('#log');

    //bind to State Change
    History.Adapter.bind(window, 'statechange', function () { // statechange 代替 popstate 
        var State = History.getState(); // History.getState() 代替 event.state
        $.ajax({
            url: State.url,
            success: function (msg) {
                $('#content').html($(msg).find('#content').html());
                $('#loading').remove();
                $('#content').fadeIn();
                docReady();
            }
        });
    });
    //菜单点击事件
    $('a.ajax-link').click(function (e) {
        if ($.browser.msie) e.which = 1;
        if (e.which != 1 || !$('#is-ajax').prop('checked') || $(this).parent().hasClass('active')) return;
        e.preventDefault();
        if ($('.btn-navbar').is(':visible')) {
            $('.btn-navbar').click();
        }
        $('#loading').remove();
        $('#content').fadeOut().parent().append('<div id="loading" class="center">载入中...<div class="center"></div></div>');
        var $clink = $(this);
        History.pushState(null, null, $clink.attr('href'));
        $('ul.main-menu li.active').removeClass('active');
        $clink.parent('li').addClass('active');
    });

    //鼠标移入菜单动画
    $('ul.main-menu li:not(.nav-header)').hover(function () {
        $(this).animate({ 'margin-left': '+=5' }, 300);
    },
	function () {
	    $(this).animate({ 'margin-left': '-=5' }, 300);
	});
    docReady();
});

function docReady() {
   
}